<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 11/6/12
 * Time: 11:54 AM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Payments_PaymentMethodsErrorCodes extends Page {

	/**
	 * @var Db_Payments_PaymentMethodsErrorCodes
	 */
	protected $tableObject;

	function __construct() {
		$this->idTableObject = get('id', 'int');
		$this->classTableObject = 'Db_Payments_PaymentMethodsErrorCodes';
		$this->actionsTableObject = array(
			'addEditAction',
			'ajaxLoadDataAction',
			'saveAction',
		);
		parent::__construct();
	}

	public function defaultAction() {
		self::$TPL->appendJsFile('admin/payment_methods_error_codes');
		$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit');
		self::$TPL->setBodyTemplate('list_payment_methods_error_codes');
		return array(
			'type' => 'display'
		);
	}

	public function addEditAction() {
		perm('PAYMENT_METHODS_ERROR_CODES_EDIT', self::$PAGE['p_url']);

		$paymentMethods = Db_Payments_PaymentMethods::findAll();
		self::$TPL->assign('paymentMethods', $paymentMethods);

		$paymentMethodsErrorRetry = Db_Payments_PaymentMethodsErrorCodes::getEnumValues('pmec_retry_possible');
		self::$TPL->assign('paymentMethodsErrorRetry', $paymentMethodsErrorRetry);

		if(!empty($this->tableObject->objectArray->id)) {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') . ' "' . $this->tableObject->objectArray->pmec_error_id . '"', '?action=addEdit', true);
			self::$TPL->assign('paymentMethodsErrorCodes', $this->tableObject->objectArray);
		} else {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit', true);
		}


		self::$TPL->setBodyTemplate('add_edit_payment_methods_error_codes');
		return array(
			'type' => 'display'
		);
	}

	public function ajaxLoadDataAction() {

		$allColumns = array('nr', 'pmec_error_id', 'pmec_description', 'pmec_retry_possible', 'pm_name');
		$searchColumns = array('pmec_error_id', 'pmec_description', 'pmec_retry_possible', 'pm_name');

		list($searchQuery, $orderBy, $limit) = Utils::getSqlQueriesForPagination($allColumns, $searchColumns, "Db_Payments_PaymentMethodsErrorCodes", "getTotalPaymentMethodsErrorCodes");

		$allPaymentMethodsErrorCodes = $this->tableObject->getAllPaymentMethodsErrorCodes($limit, $searchQuery, $orderBy);
		self::$TPL->assign('allPaymentMethodsErrorCodes', $allPaymentMethodsErrorCodes);

		return array(
			'type' => 'encoded_json',
			'content' => self::$TPL->display('json_payment_methods_error_codes', true)
		);
	}

	public function saveAction() {
		perm('PAYMENT_METHODS_ERROR_CODES_EDIT',self::$PAGE['p_url']);
		$this->message = t(T_FORM_MSG, 'success');

		$this->tableObject->pmec_error_id = post('pmec_error_id', 'str');
		$this->tableObject->pmec_description = post('pmec_description', 'str');
		$this->tableObject->pmec_payment_method_id = post('pmec_payment_method_id', 'int');
		$this->tableObject->pmec_retry_possible = post('pmec_retry_possible', 'str');

		if(!empty($this->tableObject->pmec_error_id) && !empty($this->tableObject->pmec_description) && !empty($this->tableObject->pmec_payment_method_id) && !empty($this->tableObject->pmec_retry_possible)) {
			if(!$this->tableObject->save()) {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			}
		}

		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

	public function deleteAction() {
		perm('PAYMENT_METHODS_ERROR_CODES_EDIT', self::$PAGE['p_url']);
		$this->message = t(T_FORM_MSG, 'success');

		if(!Db_Payments_PaymentMethodsErrorCodes::abstractDelete($this->idTableObject)) {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}
}
